package com.pfe.myrobot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class RobotReceiver {

	// Ports
	public static int HIT_PORT = 9998;
	public static int SHOOT_PORT = 9997;
	public static int CALL_PORT = 9996;
	public static int ANSWER_PORT = 9995;
	
	//Macros
	public static int ME = 0;
	public static int YOU = 1;

	// Runnable classes
	private CallReceiver callReceiver;
	private AnswerReceiver answerReceiver;
	private HitReceiver hitReceiver;

	// Threads
	private Thread thrCall, thrAnswer, thrHit;
	// Sockets
	private Socket sktCall, sktAnswer, sktHit;
	// Buffers
	private BufferedReader inCall, inAnswer, inHit;

	// Listeners interface
	private OnCallReceiverListener callListener;
	private OnAnswerReceiverListener answerListener;
	private OnHitReceiverListener hitListener;

	public void setOnCallReceiver(OnCallReceiverListener listener) {
		callListener = listener;
		callReceiver = new CallReceiver();
		thrCall = new Thread(callReceiver);
		thrCall.start();
	}
	
	public void removeOnCallReceiver() {
		thrCall.interrupt();
	}

	public interface OnCallReceiverListener {
		public void onCallReceiverListener(NewGameParameters newParams);
	}

	class CallReceiver implements Runnable {
		@Override
		public void run() {
			try {
				sktCall = new Socket(GameActivity.SERVER_IP, HIT_PORT);
				inCall = new BufferedReader(new InputStreamReader(sktCall.getInputStream()));
				while (!Thread.interrupted()) {
					if(inHit.ready()) {
						String input;
						input = inCall.readLine();
						if (input != null && !input.isEmpty()) {
							String[] params = input.split(";");
							NewGameParameters newParams = new NewGameParameters();
							newParams.setOtherPlayer(params[0]);
							newParams.setLifeMax(Integer.parseInt(params[1]));
							newParams.setTimeLimit(Integer.parseInt(params[2]));
							callListener.onCallReceiverListener(newParams);
						}
					}
				}
			} catch (IOException e) {
					e.printStackTrace();
			}
		}
	}

	public void setOnAnswerReceiver(OnAnswerReceiverListener listener) {
		answerListener = listener;			
		answerReceiver = new AnswerReceiver();
		thrAnswer = new Thread(answerReceiver);
		thrAnswer.start();
	}
	
	public void removeOnAnswerReceiver() {
		thrAnswer.interrupt();
	}

	public interface OnAnswerReceiverListener {
		public void onAnswerReceiverListener(String answer);
	}

	class AnswerReceiver implements Runnable {
		@Override
		public void run() {
			try {
				sktAnswer = new Socket(GameActivity.SERVER_IP, HIT_PORT);
				inAnswer = new BufferedReader(new InputStreamReader(sktAnswer.getInputStream()));
				while (!Thread.interrupted()) {
					if(inHit.ready()) {
						String input;
						input = inAnswer.readLine();
						if (input != null && !input.isEmpty()) {
							answerListener.onAnswerReceiverListener(input);					
						}
					}
				}
			} catch (IOException e) {
					e.printStackTrace();
			}
		}
	}

	public void setOnHitReceiver(OnHitReceiverListener listener) {
		hitListener = listener;
		hitReceiver = new HitReceiver();
		thrHit = new Thread(hitReceiver);
		thrHit.start();
	}
	
	public void removeOnHitReceiver() {
		thrHit.interrupt();
	}

	public interface OnHitReceiverListener {
		public void onHitReceiverListener(int player);
	}

	class HitReceiver implements Runnable {
		@Override
		public void run() {
			try {
				sktHit = new Socket(GameActivity.SERVER_IP, HIT_PORT);
				inHit = new BufferedReader(new InputStreamReader(sktHit.getInputStream()));
				while (!Thread.interrupted()) {
					if(inHit.ready()) {
						String input;				
						input = inHit.readLine();
						if (input != null && !input.isEmpty()) {
							hitListener.onHitReceiverListener(Integer.parseInt(input));
						}
					}
				}
			} catch (IOException e) {
					e.printStackTrace();
			}
		}
	}
}
